/*
 * Copyright 2016-2017 lei.xu<xulei2008xulei@163.com>.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.san21go.jfinfunc.financ.norm;

import org.san21go.jfinfunc.AbstractFunction;
import org.san21go.jfinfunc.FinFuncRuntimeException;
import org.san21go.jfinfunc.jfinfunc.math.AVERAGE;

/**
 * Alpha系数又称简森指数/詹森指数(Jensen)<br/>
 * 投资中面临着系统性风险（即BetaBeta）和非系统性风险（即AlphaAlpha ），AlphaAlpha 是投资者获得与市场波动无关的回报。<br/>
 * 比如投资者获得了15%的回报，其基准获得了10%的回报，那么AlphaAlpha 或者价值增值的部分就是5%。<br/>
 * Alpha=α=Rp−(Rf+βp(Rm−Rf))<br/>
 * Rp=策略年化收益率<br/>
 * Rm=基准年化收益率<br/>
 * Rf=无风险利率<br/>
 * βp=策略Beta值<br/>
 * Alpha值 解释<br/>
 * α>0 策略相对于市场，获得了超额收益<br/>
 * α=0 策略相对于市场，获得了适当收益<br/>
 * α<0 策略相对于市场，获得了较少收益<br/>
 * 
 * @author xulei
 *
 */
public class Alpha extends AbstractFunction<Double> {

	/**
	 * 年化收益率
	 */
	private double[] yieldRates;
	/**
	 * 基准收益率
	 */
	private double[] baseRates;

	/**
	 * 无风险收益率
	 */
	private double[] freeRiskRates;

	public Alpha(double[] yieldRates, double[] baseRates, double[] freeRiskRates) {
		super();
		this.yieldRates = yieldRates;
		this.baseRates = baseRates;
		this.freeRiskRates = freeRiskRates;
	}

	@Override
	public Double evaluate() {
		if (yieldRates == null || freeRiskRates == null || baseRates == null || baseRates.length == 0
				|| yieldRates.length == 0 || freeRiskRates.length == 0) {
			throw new FinFuncRuntimeException("参数错误");
		}
		if (yieldRates.length != baseRates.length || freeRiskRates.length != yieldRates.length) {
			throw new FinFuncRuntimeException("参数错误");
		}

		double Rf = new AVERAGE(freeRiskRates).evaluate();
		double Rp = new AVERAGE(yieldRates).evaluate();
		double Rm = new AVERAGE(baseRates).evaluate();
		double βp = new Beta(yieldRates, baseRates).evaluate();

		double alpha = Rp - (Rf + βp * (Rm - Rf));
		return alpha;
	}

}
